<template lang="">
  <div>
    <div class="ser_div">
      <el-row>
        <el-col :span="4">
          <el-button
            @click="add_model"
            style="background: #1890ff; color: aliceblue"
            >新增字段</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table :data="ModelList" stripe style="width: 100%" border>
      <el-table-column prop="field" label="字段"> </el-table-column>
      <el-table-column prop="name" label="字段名称"> </el-table-column>
      <el-table-column prop="formtype" label="字段类型"> </el-table-column>
      <el-table-column label="是否必填">
        <template slot-scope="scope">
          <span>{{ scope.row.required == 1 ? "必填" : "非必填" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否展示">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.show"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
            @change="switch_change(scope.row.show, scope.row.id)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250" fixed="right">
        <template slot-scope="scope">
          <el-button
            @click="set_ziduan(scope.row)"
            type="text"
            style="margin-left: 10px"
            >修改</el-button
          >
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除该模型吗？"
            @confirm="del_moxing(scope.row)"
          >
            <el-button slot="reference" type="text" style="margin-left: 10px"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog
      :title="title"
      :visible.sync="isProduct"
      width="30%"
      :close-on-click-modal="false"
      @close="close_diolog"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="字段 " prop="field">
          <el-input v-model="ruleForm.field" placeholder='请输入字段' style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="字段名称 " prop="name">
          <el-input v-model="ruleForm.name" placeholder='请输入字段名称'  style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="是否展示" prop="isshow">
          <el-switch
            v-model="ruleForm.isshow"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="字段类型" prop="formtype">
          <el-select
                v-model="ruleForm.formtype"
                placeholder="请选择产品"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in formtypelist"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="显示顺序 " prop="listorder">
          <el-input v-model="ruleForm.listorder" placeholder='请输入显示顺序'  style="width: 50%"></el-input>
        </el-form-item>
        <el-form-item label="是否必填 " prop="required">
          <el-switch
            v-model="ruleForm.required"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isProduct = false">取 消</el-button>
        <el-button type="primary" @click="add_Models('ruleForm')"
          >确定</el-button
        >
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { getModelFieldList } from "@/api/product/index";
export default {
  data() {
    return {
      isProduct: true,
      title:'',
      ruleForm: {
        modelname: "",
        tablename: "",
      },
      rules: {
        modelname: [
          { required: true, message: "请输入模型名称", trigger: "blur" },
        ],
        tablename: [
          { required: true, message: "请输入表名", trigger: "blur" },
        ]
      },
      ModelList: [],
      modelid:'',
      formtypelist:[
        {
          name:'输入框',
          id:'input',
        },
        {
          name:'文本框',
          id:'inptextareaut',
        },
        {
          name:'单选框',
          id:'radio',
        },
        {
          name:'下拉',
          id:'select',
        },
        {
          name:'文件上传',
          id:'file',
        },
        {
          name:'多选',
          id:'checkbox',
        }
      ]
      };
  },
  mounted() {
    console.log(this.$route.query);
    this.get_ModelFieldList(this.$route.query.data);
  },
  methods: {

    get_ModelFieldList(id) {
      this.modelid=id;
      getModelFieldList({ modelid: id }).then((res) => {
        if (res.code == 200) {
          this.ModelList = res.data;
        }
      });
    },
    set_ziduan() {},
    del_moxing() {},
    add_model() {
      this.title='添加字段'
      this.isProduct=true
    },

    switch_change(val, id) {
      let prms = {
        id,
        status: val,
      };
      setRoleStatus(prms).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.$message.success("提交成功");
          this.getlist();
        }
      });
    },
    add_Models(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addModel(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.isProduct = false;
              this.$message.success("提交成功");
              this.get_ModelFieldList(this.modelid);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
     
    },
    close_diolog(){

    }
  },
};
</script>
<style lang=""></style>
